package com.qf.grant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.canstants.Constants;
import com.qf.entity.Coupon;
import com.qf.grant.mapper.TCouponMapper;
import com.qf.grant.service.ITCouponService;
import com.qf.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Consumer;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljc
 * @since 2023-03-10
 */
@Service
public class TCouponServiceImpl extends ServiceImpl<TCouponMapper, Coupon> implements ITCouponService {



    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    TCouponMapper tCouponMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo creatCoupon(Integer amount) {

        String substring =null;
        Random random = new Random();
        List<Coupon> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Coupon coupon = new Coupon();
            substring= UUID.randomUUID().toString().replace("-", "").substring(0, 10);
            coupon.setName(substring);
            coupon.setDate(new Date());
            coupon.setRangem("无门槛");
            int i1 = random.nextInt(100)+1;
            coupon.setSumall(i1);
            list.add(coupon);
        }
            saveBatch(list);

        return ResultVo.ok();
    }

    @Override
    public ResultVo putOut() {


        List<Coupon> list = tCouponMapper.selectList(new QueryWrapper<>());

        list.forEach(new Consumer<Coupon>() {
            @Override
            public void accept(Coupon coupon) {
                redisTemplate.opsForSet().add(Constants.REDIS_COUPON_KEY, coupon);
            }
        });


        return ResultVo.ok();
    }
}
